<?php
/**
 * #增加代金券过期状态
 * ALTER TABLE `au_coupon` CHANGE `use_status` `use_status` TINYINT(1) NOT NULL COMMENT '(0,未使用 1已使用,2过期)';
 * ALTER TABLE `au_dealer_coupon` CHANGE `use_status` `use_status` TINYINT(1) NOT NULL COMMENT '0未使用，1已使用,2过期';
 *
 * #增加一个代金券消耗类型
 * INSERT INTO `au_coupon_type` (`coupon_type_id`, `type_name`, `amount`) VALUES ('9', '2016年1月车主违约补偿', '500');
 * #表重命名
 * RENAME TABLE `au_dealer_coupon_cost_log` TO `au_coupon_log`;
 * #增加消耗日志记录代金券编号和id
 * ALTER TABLE `au_coupon_log` ADD COLUMN `coupon_id` INT NOT NULL COMMENT '代金券ID' AFTER `order_id`, ADD COLUMN `coupon_no` VARCHAR(30) NULL COMMENT '代金券编号' AFTER `coupon_id`;
 * ALTER TABLE `au_coupon_log` CHANGE `coupon_no` `coupon_no` VARCHAR(30) CHARSET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '代金券编号';
 * ALTER TABLE `au_coupon_log` ADD COLUMN `occur_type` INT NOT NULL COMMENT '1发放,2使用,3过期' AFTER `coupon_no`;
 * ALTER TABLE `au_coupon_log` CHANGE `occur_type` `occur_type` INT(11) NOT NULL COMMENT '0发放,1使用,2过期';
 * ALTER TABLE `au_coupon_log` DROP `dealer_confirm_status`;
 *
 * #需要处理的地方
 * #1.发放代金券插入记录(运营平台)
 * #2.显示代金券记录(运营平台)
 * #3.使用代金券插入记录(拍卖平台)
 * #4.显示代金券记录(拍卖平台)
 * #5.代金券过期服务(后台进程)
 * #6.建立旧的代金券使用记录
 */
#error_reporting ( 0 );
date_default_timezone_set("Asia/Shanghai");
set_time_limit(0);
if (isset ($_SERVER ['REQUEST_URI'])) {
    die ('error');
}
if (!defined('API_ROOT_PATH')) {
    define('API_ROOT_PATH', dirname(__FILE__));
}

require_once(API_ROOT_PATH . '/../inc/common.php');

while (true) {
    $today = date("Y-m-d");
    $db = get_mysqli_db();
    //未发放的代金券
    $row1 = $db->where('end_date', $today, "<")
        ->where('assign_status', 0)
        ->getOne('coupon');
    //已发放的代金券
    $row = $db->where('end_date', $today, "<")
        ->where('use_status',0)
        ->getOne('dealer_coupon');

    if (!$row && !$row1) {
        $sleeptimes = getNextSleepSecond();
        print_r("sleep:" . $sleeptimes . "\n");
        sleep($sleeptimes);
        continue;
    }
    //已发放的代金券
    if ($row) {
        $now = date("Y-m-d H:i:s", time());
        $db->where('dc_id', $row['dc_id'])
            ->update('dealer_coupon', [
                'use_status' => 2
            ]);
        //新增代金券消费记录
        $db->insert('coupon_log', [
            'dealer_id' => $row['dealer_id'],
            'cost_type' => '过期',
            'occur_time' => $now,
            'occur_money' => $row['amount'],
            'remark' => '代金券过期',
            'createtime' => $now,
            'order_id' => 0,
            'coupon_id' => $row['coupon_id'],
            'coupon_no' => $row['coupon_no'],
            'occur_type' => 2,
        ]);
        logUtil($row['coupon_no'] . '代金券使用过期', "INFO");
    }
    //未发放的
    if ($row1) {
        $db->where('coupon_id', $row1['coupon_id'])
            ->update('coupon', [
                'assign_status' => 2,
            ]);
        logUtil($row1['coupon_no'] . '代金券发放过期', 'INFO');
    }

    //exit;
}

function getNextSleepSecond()
{
    $date1 = time();
    $date2 = date('Y-m-d 1:0:00', strtotime('+1 day', $date1));

    $date2 = strtotime($date2);
    $s = $date2 - $date1;

    $date1 = date('Y-m-d H:i:s', $date1);
    $date2 = date('Y-m-d H:i:00', $date2);

    print_r("currtime:$date1   nexttime:$date2   timediff:$s\n");

    return $s;
}
